package com.osdp.sc.rest;

import com.osdp.sc.dto.PermissionDTO;
import com.osdp.sc.dto.TenantUserDTO;
import com.osdp.sc.service.IScPermissionService;
import com.osdp.sc.service.IScRoleService;
import com.osdp.sc.service.IScRoleUserService;
import com.osdp.sc.service.IScTenantUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.Set;

@RestController
@RequestMapping("/v1/rest")
@Api(tags = "Feign接口管理")
@Slf4j
public class ScServiceRest {

    @Autowired
    private IScRoleService roleService;

    @Autowired
    private IScPermissionService permissionService;

    @Autowired
    private IScRoleUserService roleUserService;

    @Autowired
    private IScTenantUserService tenantUserService;

    /**
     *
     * @param uri
     * @param method
     * @param authoritys
     * @return
     */
    @PostMapping("/auth/allow")
    public boolean allow(@RequestParam("uri") String uri,@RequestParam("method") String method,
                         @RequestBody Set<String> authoritys){
        return permissionService.allow(uri,method,authoritys);
    }

    /**
     * 根据用户ID查询对应的角色id
     * @param userId
     * @return
     */
    @ApiOperation(value = "根据用户ID获取角色信息", notes = "获取角色信息")
    @GetMapping("/role/roleIdsByUserId")
    public List<String> queryRoleIdByUserId(@RequestParam(value = "userId") String userId){
        return roleUserService.queryRoleIdByUserId(userId);
    }

    /**
     * 根据用户ID查询角色编码
     * @param userId
     * @return
     */
    @GetMapping("/role/roleCodes")
    public List<String> getRoleCodeByUserId(@RequestParam(value = "userId") String userId){
        return roleService.getRoleCodeByUserId(userId);
    }

    /**
     * 根据用户ID 查询权限
     * @param userId String
     * @return List<ScPermission>
     */
    @GetMapping("/perms/permsByUserId")
    public List<PermissionDTO> queryPermissionsByUserId(@RequestParam(value = "userId")String userId){
        return permissionService.queryPermissionsByUserId(userId);
    }

    /**
     * 查询所有按钮资源
     * @return List<PermissionVo>
     */
    @GetMapping("/perms/permsButByUserId")
    public List<PermissionDTO> queryPermissionButtonByUserId(@RequestParam(value = "userId") String userId){
        return permissionService.queryPermissionButtonByUserId(userId);
    }

    /**
     * 查询API权限集合
     * @param userId String 用户ID
     * @return List<PermissionVo>
     */
    @GetMapping("/perms/permsApiByUserId")
    public List<PermissionDTO> queryPermissionAPIByUserId(@RequestParam(value = "userId") String userId){
        return permissionService.queryPermissionAPIByUserId(userId);
    }

    /**
     * 根据角色编码查询所有按钮与API资源
     * @param roleCodes Set<String>
     * @param tenantId String
     * @return Optional<List<PermissionDTO>>
     */
    @PostMapping("/perms/permsButAndApis")
    public Optional<List<PermissionDTO>> queryPermsButtonAndAPIByRoleCode(
            @RequestBody Set<String> roleCodes,
            @RequestParam(value = "tenantId") String tenantId){
        return permissionService.queryPermsButtonAndAPIByRoleCode(roleCodes,tenantId);
    }

    @GetMapping("/perms/permsCodeByUserId")
    public List<String> queryPermsCodeByUserId(@RequestParam(value = "userId")  String userId){
        return permissionService.queryPermsCodeByUserId(userId);
    }
    /**
     * 根据用户ID得到租户ID
     * @param userId
     * @return
     */
    @GetMapping("/tenant/tenantsByUserId")
    public List<TenantUserDTO> queryTenantByUserId(@RequestParam(value = "userId") String userId){
        return tenantUserService.queryTenantByUserId(userId);
    }

    /**
     * 获取与与租户编码一致的租户id
     *
     * @param tenants
     * @param tenantCode
     * @return
     */
    @PostMapping("/tenant/currentTenantId")
    public String getCurrentTenantId(@RequestBody List<TenantUserDTO> tenants,
                                             @RequestParam(value = "tenantCode")String tenantCode){
        return tenantUserService.getCurrentTenantId(tenants,tenantCode);
    }

}
